package com.ideal.manage.esms.controller.report;


import com.alibaba.fastjson.JSONObject;
import com.ideal.manage.esms.bean.DTO.PageDto;
import com.ideal.manage.esms.bean.alarm.Alarm;
import com.ideal.manage.esms.bean.report.ReporTearnings;
import com.ideal.manage.esms.bean.report.Report;
import com.ideal.manage.esms.service.alarm.AlarmService;
import com.ideal.manage.esms.service.monitor.MonitorService;
import com.ideal.manage.esms.service.report.ReportService;
import com.ideal.manage.esms.service.report.ReportTearningsService;
import com.ideal.manage.esms.service.report.ReportYearService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.List;

@Controller
@RequestMapping("report")
public class ReportController {
    @Resource
    private ReportTearningsService reportTearningsService;
    @Resource
    private AlarmService alarmService;
    @Resource
    private MonitorService monitorService;
    @Resource
    private ReportService reportService;
    @Resource
    private ReportYearService reportYearService;

    /**
     * 报表列表请求
     *
     * @param model
     */
    @RequestMapping("report_list")
    public void reportList(Model model) {


    }

    /**
     * 报表列表数据
     *
     * @return
     */
    @RequestMapping("report_data")
    @ResponseBody
    public PageDto reportData(int pageNum, HttpServletRequest request) {
        PageDto pageDto = reportService.findReportData(pageNum, request);
        return pageDto;
    }

    /**
     * 月报表详情
     *
     * @param id
     */
    @RequestMapping("report_detail")
    public void reportDetail(Model model,
                             @RequestParam(value = "id", required = false) String id, String name) {

        Report report = reportService.findCreateTime(name);

        model.addAttribute("report", report);

        List<ReporTearnings> reportLists = reportTearningsService.findAll();

        model.addAttribute("reportLists", reportLists);



    }

    /**
     * 年报报表请求
     *
     * @param model
     * @param id
     */
    @RequestMapping("report_detail_year")
    public void reportDetailYesr(Model model,
                                 @RequestParam(value = "id", required = false) String id, String name) {
        Report reportYear = reportService.findCreateTimeByYear(name);

        model.addAttribute("reportYear", reportYear);
        // 收益表及环保贡献
        List<ReporTearnings> reportLists = reportTearningsService.findAll();

        model.addAttribute("reportLists", reportLists);


    }


    /**
     * 警告列表请求
     *
     * @param pageNum
     * @param request
     * @return
     */
    @RequestMapping("report_alarm_data")
    @ResponseBody
    public PageDto alarmData(int pageNum, HttpServletRequest request) {
        PageDto pageDto = null;
        try {
            pageDto = alarmService.findMothData(pageNum, request);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return pageDto;
    }

    /**
     * 年报报警请求
     *
     * @param pageNum
     * @param request
     * @return
     */
    @RequestMapping("report_alarm_data_year")
    @ResponseBody
    public PageDto alarmDataYear(int pageNum, HttpServletRequest request) {
        PageDto pageDto = null;
        try {
            pageDto = alarmService.findYearReport(pageNum, request);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return pageDto;
    }


    /**
     * 报警等级数量
     */
    @RequestMapping("alarm_scale")
    @ResponseBody
    public List<JSONObject> alarmLev(String id) {
        //报警等级数量
        List<Alarm> alarmList = alarmService.findAlarmEquipment(id);
        return  alarmService.findLevelByAlarmMonth(alarmList);
    }

    /**
     * 月报----->单体阻值频数
     *
     * 单体阻值频数/频率分布图
     *
     * @param id
     * @return
     */
    @RequestMapping("dcir_correcting")
    @ResponseBody
    public JSONObject monthDcir_correcting(String id) {
        //通过项目id 找到相应的电池包
        List<String> equipmentManages = reportService.findProjectsById(id);
        for (int i = 0; i < equipmentManages.size(); i++) {
            String eid = equipmentManages.get(i);
           // return monitorService.findFrequencyList(eid, "zlnzdcir");

        }
        return null;
    }

    /**
     * 月报----->频率分布图
     * 单体阻值频数/频率分布图
     * @param id
     * @return
     */
    @RequestMapping("soc_correcting")
    @ResponseBody
    public JSONObject socCorrecting(String id){
        //通过项目id 找到相应的电池包
        List<String> equipmentManages = reportService.findProjectsById(id);
        for (int i = 0; i < equipmentManages.size(); i++) {
            String eid = equipmentManages.get(i);
            return monitorService.findFrequencyList(eid, "dtsoc");  // 单体SOC
        }
        return null;
    }

    /**
     * 月报--->累计节省电费
     * @return
     */
    @RequestMapping("electricity_spare")
    @ResponseBody
    public List<JSONObject> electricitySpare(@RequestParam(value = "id")  String id){
        //先找到所有项目下的电池包
        List<String> equipmentManages = reportService.findProjectsById(id);
        //将所有的电池包信息传给获取节省电费的方法
        List<JSONObject> lists =  reportService.getElectricitySpare(equipmentManages);
        return lists;
    }

}
